package com.yixun.qfbao.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yixun.common.exception.YIXUNExceptionCode;
import com.yixun.common.page.Page;
import com.yixun.common.utils.StringUtil;
import com.yixun.qfbao.api.service.DeptPermissionService;
import com.yixun.qfbao.api.service.ReportCommissionInfoService;
import com.yixun.qfbao.apidto.*;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.ReportCommission;
import com.yixun.qfbao.model.ReportCommissionLog;
import com.yixun.qfbao.model.ReportCommissionLogImage;
import com.yixun.qfbao.model.dto.CommissionSummaryDto;
import com.yixun.qfbao.model.dto.CustomerPremisesCommissionDto;
import com.yixun.qfbao.model.dto.ReportCommissionLogDto;
import com.yixun.qfbao.model.enums.CommissionResourceEnum;
import com.yixun.qfbao.model.enums.CommissionStatusEnum;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.model.enums.OperationTypeEnum;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.service.ReportCommissionLogImageService;
import com.yixun.qfbao.service.ReportCommissionLogService;
import com.yixun.qfbao.service.ReportCommissionService;
import com.yixun.qfbao.util.PageUtil;
import com.yixun.qfbao.xf.model.CustomerPremises;
import com.yixun.qfbao.xf.model.dto.CustomerPremisesDto;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import com.yixun.qfbao.xf.service.CustomerPremisesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: channel-parent
 *
 * @description:
 *
 * @author: Seven.wang
 *
 * @create: 2020-06-29 18:56
 **/
@Service("reportCommissionInfoService")
public class ReportCommissionInfoServiceImpl implements ReportCommissionInfoService {

    private Logger logger = LoggerFactory.getLogger(ReportCommissionInfoServiceImpl.class);

    @Autowired
    private ReportCommissionService reportCommissionService;

    @Autowired
    private ReportCommissionLogService reportCommissionLogService;

    @Autowired
    private ReportCommissionLogImageService reportCommissionLogImageService;

    @Autowired
    private CustomerPremisesService customerPremisesService;

    @Autowired
    private PartnerInfoService partnerInfoService;
    @Autowired
    private DeptPermissionService deptPermissionService;

    @Override
    public EditReportCommissionResDto editReportCommission(EditReportCommissionReqDto reqDto) {
        EditReportCommissionResDto resDto = new EditReportCommissionResDto();
        if(reqDto.getCommission() == null || BigDecimal.ZERO.compareTo(reqDto.getCommission()) > 0){
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("请输入正确的佣金总额，且金额不能小于0!");
            return resDto;
        }
        CustomerPremises cp = customerPremisesService.selectById(reqDto.getReportId());
        if("add".equals(reqDto.getSaveType())){
            try {
                CustomerPremisesDto cpDto = new CustomerPremisesDto();
                BeanUtils.copyProperties(cp, cpDto);
                logger.info("operateCommission subscribe info {}", JSONObject.toJSONString(cpDto));
                ReportCommission rc = new ReportCommission();
                initReportCommission(rc, cpDto);
                calculationEditCommission(rc,reqDto );
                rc = reportCommissionService.addForReport(rc, Long.parseLong(reqDto.getUserId()));
                //新增备注，图片
                if(CommissionResourceEnum.AGENT.getNo().equals(reqDto.getCommissionType()) && reqDto.getCommission() != null){
                    cp.setCommissionStatus(cp.getCommissionStatus() == null ? com.yixun.qfbao.xf.model.enums.CommissionStatusEnum.no_commission.getNo() : cp.getCommissionStatus());
                    updateCustomerPremisesCommission(cp, rc.getAgentCommission());
                }
                // TODO: 2020/7/7   需要新增备注，图片
                ReportCommissionLog log = new ReportCommissionLog();
                log.setCommissionStatus(com.yixun.qfbao.model.enums.CommissionStatusEnum.COMMISSIONSTATUS_VALUE_0.getCode());
                // 添加编辑记录
                log.setCommission(reqDto.getCommission() == null ? BigDecimal.ZERO : reqDto.getCommission());
                log.setReportId(rc.getReportId());
                log.setReportCommissionId(rc.getId());
                log.setCreateUserId(reqDto.getUserId() == null?null:Long.parseLong(reqDto.getUserId()));
                log.setCreateTime(new Date());
                log.setCommissionType(reqDto.getCommissionType());
                log.setDelFlag(DelFlagEnum.UNDEL.getNo());
                log.setOperationType(OperationTypeEnum.OPERATIONTYPE_0.getCode());
                log.setRemark(reqDto.getRemark());
                log = reportCommissionLogService.insert(log);
                saveAffix(rc.getReportId(), reqDto.getImageList(), log);
                resDto.setResult(ResultEnum.SUCCESS.getNo());
                resDto.setResultMsg("佣金保存成功!");
                return resDto;
            } catch (Exception e){
                logger.error("operateCommission subscribe is error: {}", e);
                resDto.setResult(ResultEnum.FAIL.getNo());
                resDto.setResultMsg("报备佣金信息保存异常!");
                return resDto;
            }
        }
        ReportCommission reportCommission =  reportCommissionService.selectById(reqDto.getId());
        if(reportCommission == null){
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("报备佣金信息不存在!");
            return resDto;
        }

        ReportCommissionLog log = new ReportCommissionLog();
        if(CommissionResourceEnum.DEVELOPERS.getNo().equals(reqDto.getCommissionType())){
            if(reportCommission.getDevReceivedCommission().compareTo(reqDto.getCommission()) > 0){
                resDto.setResult(ResultEnum.FAIL.getNo());
                resDto.setResultMsg("开发商佣金总额不能大于已结佣金金额!");
                return resDto;
            }
            log.setCommissionStatus(reportCommission.getDevCommissionStatus());
        }else if(CommissionResourceEnum.AGENT.getNo().equals(reqDto.getCommissionType())){
            if(reportCommission.getAgentReceivedCommission().compareTo(reqDto.getCommission()) > 0){
                resDto.setResult(ResultEnum.FAIL.getNo());
                resDto.setResultMsg("合作机构佣金总额不能大于已结佣金金额!");
                return resDto;
            }
            log.setCommissionStatus(reportCommission.getAgentCommissionStatus());
        }
        if(reportCommission.getDevCompanyId() == null){
            PartnerInfo pi = partnerInfoService.selectById(Long.parseLong(reqDto.getUserId()));
            reportCommission.setDevCompanyId(pi.getHigherid());
        }
        calculationEditCommission(reportCommission, reqDto);
        reportCommission.setUpdateUserId(Long.parseLong(reqDto.getUserId()));
        reportCommission.setUpdateTime(new Date());
        reportCommissionService.update(reportCommission);
        if(CommissionResourceEnum.AGENT.getNo().equals(reqDto.getCommissionType()) && reqDto.getCommission() != null){
            cp.setCommissionStatus(cp.getCommissionStatus() == null ? com.yixun.qfbao.xf.model.enums.CommissionStatusEnum.no_commission.getNo() : cp.getCommissionStatus());
            updateCustomerPremisesCommission(cp, reportCommission.getAgentCommission());
        }
        // 添加编辑记录
        log.setCommission(reqDto.getCommission() == null ? BigDecimal.ZERO : reqDto.getCommission());
        log.setReportId(reportCommission.getReportId());
        log.setReportCommissionId(reportCommission.getId());
        log.setCreateUserId(reqDto.getUserId() == null?null:Long.parseLong(reqDto.getUserId()));
        log.setCreateTime(new Date());
        log.setCommissionType(reqDto.getCommissionType());
        log.setDelFlag(DelFlagEnum.UNDEL.getNo());
        log.setOperationType(OperationTypeEnum.OPERATIONTYPE_0.getCode());
        log.setRemark(reqDto.getRemark());
        log = reportCommissionLogService.insert(log);

        /*
         * 保存跟进附件
         */
        saveAffix(reportCommission.getReportId(), reqDto.getImageList(), log);
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setResultMsg("佣金编辑成功!");
        return resDto;
    }

    @Override
    public DealReportCommissionResDto dealReportCommission(DealReportCommissionReqDto reqDto) {
        DealReportCommissionResDto resDto = new DealReportCommissionResDto();
        ReportCommission reportCommission =  reportCommissionService.selectById(reqDto.getId());
        if(reportCommission == null){
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("报备佣金信息不存在!");
            return resDto;
        }
        if(CommissionResourceEnum.DEVELOPERS.getNo().equals(reqDto.getCommissionType())){
            if(reportCommission.getDevCommission().compareTo(reqDto.getCommission()) < 0){
                resDto.setResult(ResultEnum.FAIL.getNo());
                resDto.setResultMsg("开发商结算佣金不能大于佣金总额!");
                return resDto;
            }
            if(reportCommission.getDevPendingCommission().compareTo(reqDto.getCommission()) < 0){
                resDto.setResult(ResultEnum.FAIL.getNo());
                resDto.setResultMsg("开发商结算佣金不能大于待结佣金!");
                return resDto;
            }
            reportCommission.setDevCommissionStatus(reqDto.getCommissionStatus());
        }else if(CommissionResourceEnum.AGENT.getNo().equals(reqDto.getCommissionType())){
            if(reportCommission.getAgentCommission().compareTo(reqDto.getCommission()) < 0){
                resDto.setResult(ResultEnum.FAIL.getNo());
                resDto.setResultMsg("合作机构结算佣金不能大于佣金总额!");
                return resDto;
            }
            if(reportCommission.getAgentPendingCommission().compareTo(reqDto.getCommission()) < 0){
                resDto.setResult(ResultEnum.FAIL.getNo());
                resDto.setResultMsg("合作机构结算佣金不能大于待结佣金!");
                return resDto;
            }
            reportCommission.setAgentCommissionStatus(reqDto.getCommissionStatus());
        }
        calculationDealCommission(reportCommission, reqDto);
        reportCommission.setUpdateUserId(Long.parseLong(reqDto.getUserId()));
        reportCommission.setUpdateTime(new Date());
        reportCommissionService.update(reportCommission);
        CustomerPremises cp = customerPremisesService.selectById(reportCommission.getReportId());
        if(CommissionResourceEnum.AGENT.getNo().equals(reqDto.getCommissionType()) && reqDto.getCommission() != null){
            if(com.yixun.qfbao.xf.model.enums.CommissionStatusEnum.has_commission.getNo().equals(reqDto.getCommissionStatus()) && com.yixun.qfbao.xf.model.enums.CommissionStatusEnum.no_commission.getNo().equals(cp.getCommissionStatus()) ){
                cp.setCommissionStatus(com.yixun.qfbao.xf.model.enums.CommissionStatusEnum.has_commission.getNo());
            }else if(cp.getCommissionStatus() == null){
                cp.setCommissionStatus(com.yixun.qfbao.xf.model.enums.CommissionStatusEnum.no_commission.getNo());
            }
            updateCustomerPremisesCommission(cp, reportCommission.getAgentCommission());
        }

        // 添加编辑记录
        ReportCommissionLog log = new ReportCommissionLog();
        log.setCommission(reqDto.getCommission() == null ? BigDecimal.ZERO : reqDto.getCommission());
        log.setCommissionStatus(reqDto.getCommissionStatus());
        log.setReportId(reportCommission.getReportId());
        log.setReportCommissionId(reportCommission.getId());
        log.setCreateUserId(reqDto.getUserId() == null?null:Long.parseLong(reqDto.getUserId()));
        log.setCreateTime(new Date());
        log.setCommissionType(reqDto.getCommissionType());
        log.setDelFlag(DelFlagEnum.UNDEL.getNo());
        log.setOperationType(OperationTypeEnum.OPERATIONTYPE_1.getCode());
        log.setRemark(reqDto.getRemark());
        log = reportCommissionLogService.insert(log);

        /*
         * 保存跟进附件
                */
        saveAffix(reportCommission.getReportId(), reqDto.getImageList(), log);
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setResultMsg("审核成功!");
        return resDto;
    }


    @Override
    public GetReportCommissionLogResDto getReportCommissionLogList(ReportCommissionLogReqDto reqDto) {
        GetReportCommissionLogResDto resDto = new GetReportCommissionLogResDto();
        ReportCommissionLog entity = new ReportCommissionLog();
        entity.setReportCommissionId(reqDto.getId());
        entity.setCommissionType(reqDto.getCommissionType());
        entity.setDelFlag(DelFlagEnum.UNDEL.getNo());
        List<ReportCommissionLogDto> list = reportCommissionLogService.selectReportCommissionLogList(entity);
        if(CollectionUtils.isEmpty(list)){
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("无历史记录");
            return resDto;
        }
        List<ReportCommissionLogResDto> data = list.stream().map(item -> {
            ReportCommissionLogResDto dto = new ReportCommissionLogResDto();
            BeanUtils.copyProperties(item, dto);
            List<ReportCommissionLogImage> images =  reportCommissionLogImageService.seletImageListByLogId(item.getId());
            dto.setAffixList(images);
            return dto;
        }).collect(Collectors.toList());
        resDto.setData(data);
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setResultMsg("success");
        return resDto;
    }

    /**
     * 保存跟进附件
     */
    private void saveAffix(Long reportId,List<ReportCommissionLogImageDto> affixList, ReportCommissionLog log){
        if(!CollectionUtils.isEmpty(affixList)){
            List<ReportCommissionLogImage> imageList = new ArrayList<>();
            for(ReportCommissionLogImageDto dto:affixList){
                ReportCommissionLogImage img = new ReportCommissionLogImage();
                dto.setCommissionLogId(log.getId());
                dto.setReportId(reportId);
                dto.setImageUuid(UUID.randomUUID().toString());
                dto.setAffixType("edit");
                BeanUtils.copyProperties(dto, img);
                img.setDeleted(Boolean.FALSE);
                img.setCreatedTime(new Date());
                imageList.add(img);
            }
            reportCommissionLogImageService.addImageBatch(imageList);
        }
    }

    /** 计算编辑佣金金额 */
    private void calculationEditCommission(ReportCommission reportCommission, EditReportCommissionReqDto reqDto){
        if(CommissionResourceEnum.DEVELOPERS.getNo().equals(reqDto.getCommissionType())){
            reportCommission.setDevCommission(reqDto.getCommission());
            reportCommission.setDevPendingCommission(reportCommission.getDevCommission().subtract(reportCommission.getDevReceivedCommission()));
        }else if(CommissionResourceEnum.AGENT.getNo().equals(reqDto.getCommissionType())){
            reportCommission.setAgentCommission(reqDto.getCommission());
            reportCommission.setAgentPendingCommission(reportCommission.getAgentCommission().subtract(reportCommission.getAgentReceivedCommission()));
        }
        reportCommission.setOrgCommission(reportCommission.getDevCommission().subtract(reportCommission.getAgentCommission()));
        reportCommission.setOrgPendingCommission(reportCommission.getDevPendingCommission().subtract(reportCommission.getAgentPendingCommission()));
    }

    private void initReportCommission(ReportCommission rc, CustomerPremisesDto cpDto){
        rc.setDevCommission(rc.getDevCommission() == null ? BigDecimal.ZERO : rc.getDevCommission());
        rc.setDevPendingCommission(rc.getDevPendingCommission() == null ? BigDecimal.ZERO : rc.getDevPendingCommission());
        rc.setDevReceivedCommission(rc.getDevReceivedCommission() == null ? BigDecimal.ZERO : rc.getDevReceivedCommission());
        rc.setAgentCommission(rc.getAgentCommission() == null ? BigDecimal.ZERO : rc.getAgentCommission());
        rc.setAgentPendingCommission(rc.getAgentPendingCommission() == null ? BigDecimal.ZERO : rc.getAgentPendingCommission());
        rc.setAgentReceivedCommission(rc.getAgentReceivedCommission() == null ? BigDecimal.ZERO : rc.getAgentReceivedCommission());
        rc.setOrgCommission(rc.getOrgCommission() == null ? BigDecimal.ZERO : rc.getOrgCommission());
        rc.setOrgPendingCommission(rc.getOrgPendingCommission() == null ? BigDecimal.ZERO : rc.getOrgPendingCommission());
        rc.setOrgReceivedCommission(rc.getOrgReceivedCommission() == null ? BigDecimal.ZERO : rc.getOrgReceivedCommission());
        rc.setDevCommissionStatus(CommissionStatusEnum.COMMISSIONSTATUS_VALUE_0.getCode());
        rc.setAgentCommission(rc.getAgentCommission() == null ? BigDecimal.ZERO : rc.getAgentCommission());
        rc.setAgentPendingCommission(rc.getAgentPendingCommission() == null ? BigDecimal.ZERO : rc.getAgentPendingCommission());
        rc.setAgentReceivedCommission(rc.getAgentReceivedCommission() == null ? BigDecimal.ZERO : rc.getAgentReceivedCommission());
        rc.setAgentCommissionStatus(CommissionStatusEnum.COMMISSIONSTATUS_VALUE_0.getCode());
        rc.setOrgCommission(rc.getOrgCommission() == null ? BigDecimal.ZERO : rc.getOrgCommission());
        rc.setOrgPendingCommission(rc.getOrgPendingCommission() == null ? BigDecimal.ZERO : rc.getOrgPendingCommission());
        rc.setOrgReceivedCommission(rc.getOrgReceivedCommission() == null ? BigDecimal.ZERO : rc.getOrgReceivedCommission());
        rc.setDevCommissionStatus(CommissionStatusEnum.COMMISSIONSTATUS_VALUE_0.getCode());
        rc.setAgentCommissionStatus(CommissionStatusEnum.COMMISSIONSTATUS_VALUE_0.getCode());
        rc.setAgentCompanyId(cpDto.getStaffCompanyId());
        rc.setPremisesId(cpDto.getPremisesId());
        rc.setReportId(cpDto.getId());
        rc.setDelFlag(DelFlagEnum.UNDEL.getNo());
        rc.setCreateTime(new Date());
    }

    /** 计算结算佣金金额 */
    private void calculationDealCommission(ReportCommission reportCommission, DealReportCommissionReqDto reqDto){
        if(CommissionResourceEnum.DEVELOPERS.getNo().equals(reqDto.getCommissionType())){
            reportCommission.setDevReceivedCommission(reportCommission.getDevReceivedCommission().add(reqDto.getCommission()));
            reportCommission.setDevPendingCommission(reportCommission.getDevCommission().subtract(reportCommission.getDevReceivedCommission()));
        }else if(CommissionResourceEnum.AGENT.getNo().equals(reqDto.getCommissionType())){
            reportCommission.setAgentReceivedCommission(reportCommission.getAgentReceivedCommission().add(reqDto.getCommission()));
            reportCommission.setAgentPendingCommission(reportCommission.getAgentCommission().subtract(reportCommission.getAgentReceivedCommission()));
        }
        reportCommission.setOrgCommission(reportCommission.getDevCommission().subtract(reportCommission.getAgentCommission()));
        reportCommission.setOrgPendingCommission(reportCommission.getDevPendingCommission().subtract(reportCommission.getAgentPendingCommission()));
        reportCommission.setOrgReceivedCommission(reportCommission.getOrgCommission().subtract(reportCommission.getOrgPendingCommission()));
    }

    @Override
    public AgentCommissionSummaryResDto agentCommissionSummaryPage(AgentCommissionSummaryReqDto reqDto) {
        AgentCommissionSummaryResDto resDto = new AgentCommissionSummaryResDto();
        Page<CommissionSummaryDto> pageModel = new Page<CommissionSummaryDto>();
        CommissionSummaryDto entity = new CommissionSummaryDto();
        entity.setPremisesId(reqDto.getPremisesId());
        PartnerInfo pi = partnerInfoService.selectById(Long.parseLong(reqDto.getUserId()));
        entity.setDevCompanyId(pi.getHigherid());
        if(!StringUtil.isEmpty(reqDto.getAgentOrgId())){
            entity.setAgentCompanyId(Long.parseLong(reqDto.getAgentOrgId()));
        }
        if(!StringUtil.isEmpty(reqDto.getAgentCommissionStatus())){
            entity.setAgentIsSettle(reqDto.getAgentCommissionStatus());
        }
        if(!StringUtil.isEmpty(reqDto.getDevCommissionStatus())){
            entity.setDevIsSettle(reqDto.getDevCommissionStatus());
        }

        try {
            Map<String,Integer> map = PageUtil.getPageInfo(reqDto.getPageNo(), reqDto.getPageSize());

            // 机构权限.直接以机构来查询.
            List<Long> deptPermissionList = deptPermissionService.getPartnerDeptPermission(pi.getId());
            entity.setAgencyDeptIds(deptPermissionList);
            pageModel = reportCommissionService.selectCommissionSummaryDtoList(entity,map.get("startRow"),map.get("endRow"),pi.getAccountType(),reqDto.getUserId());
            pageModel.setPageNumber(reqDto.getPageNo());
            pageModel.setPageSize(reqDto.getPageSize());
            pageModel.setTotalPages(PageUtil.getTotalPage(new Integer(pageModel.getTotal()+""), reqDto.getPageSize()));
            resDto.setPage(pageModel);
            resDto.setResult(ResultEnum.SUCCESS.getNo());
        } catch (Exception e){
            resDto.setResult(ResultEnum.FAIL.getNo());
            logger.error("分页信息失败 {}",e);
            pageModel = new Page<CommissionSummaryDto>();
            pageModel.setErrorMsg(YIXUNExceptionCode.UNKNOW_EXCEPTON.getDesout());
            resDto.setPage(pageModel);
        }
        return resDto;
    }

    @Override
    public AgentCustomerPremisesCommissionResDto agentCustomerPremisesCommissionPage(AgentCustomerPremisesCommissionReqDto reqDto) {
        AgentCustomerPremisesCommissionResDto resDto = new AgentCustomerPremisesCommissionResDto();
        Page<CustomerPremisesCommissionDto> pageModel = new Page<CustomerPremisesCommissionDto>();
        CustomerPremisesCommissionDto entity = new CustomerPremisesCommissionDto();
        entity.setPremisesId(reqDto.getPremisesId());
        PartnerInfo pi = partnerInfoService.selectById(Long.parseLong(reqDto.getUserId()));
        entity.setDevCompanyId(pi.getHigherid());
        if(!StringUtil.isEmpty(reqDto.getAgentOrgId())){
            entity.setAgentCompanyId(Long.parseLong(reqDto.getAgentOrgId()));
        }
        try {
            Map<String,Integer> map = PageUtil.getPageInfo(reqDto.getPageNo(), reqDto.getPageSize());
            // 机构权限.直接以机构来查询.
            List<Long> deptPermissionList = deptPermissionService.getPartnerDeptPermission(pi.getId());
            entity.setAgencyDeptIds(deptPermissionList);
            pageModel = reportCommissionService.selectCustomerPremisesCommissionDtoList(entity,map.get("startRow"),map.get("endRow"));
            pageModel.setPageNumber(reqDto.getPageNo());
            pageModel.setPageSize(reqDto.getPageSize());
            pageModel.setTotalPages(PageUtil.getTotalPage(new Integer(pageModel.getTotal()+""), reqDto.getPageSize()));
            resDto.setPage(pageModel);
            resDto.setResult(ResultEnum.SUCCESS.getNo());
        } catch (Exception e){
            resDto.setResult(ResultEnum.FAIL.getNo());
            logger.error("分页信息失败 {}",e);
            pageModel = new Page<CustomerPremisesCommissionDto>();
            pageModel.setErrorMsg(YIXUNExceptionCode.UNKNOW_EXCEPTON.getDesout());
            resDto.setPage(pageModel);
        }
        return resDto;
    }

    @Override
    public AgentCommissionByPremisesAndCompanyResDto agentCommissionSummary(AgentCommissionByPremisesAndCompanyReqDto reqDto) {
        AgentCommissionByPremisesAndCompanyResDto resDto = new AgentCommissionByPremisesAndCompanyResDto();
        CommissionSummaryDto entity = new CommissionSummaryDto();
        PartnerInfo pi = partnerInfoService.selectById(Long.parseLong(reqDto.getUserId()));
        entity.setDevCompanyId(pi.getHigherid());
        entity.setPremisesId(reqDto.getPremisesId());
        if(!StringUtil.isEmpty(reqDto.getAgentOrgId())){
            entity.setAgentCompanyId(Long.parseLong(reqDto.getAgentOrgId()));
        }
        // 机构权限.直接以机构来查询.
        List<Long> deptPermissionList = deptPermissionService.getPartnerDeptPermission(pi.getId());
        entity.setAgencyDeptIds(deptPermissionList);
        CommissionSummaryDto dto =reportCommissionService.selectCommissionByPremisesAndCompany(entity);
        resDto.setResult(ResultEnum.SUCCESS.getNo());
        resDto.setData(dto);
        return resDto;
    }

    /** 修改报备信息佣金金额 **/
    private void updateCustomerPremisesCommission(CustomerPremises cp, BigDecimal commission){
        CustomerPremisesDto entity = new CustomerPremisesDto();
        entity.setId(cp.getId());
        entity.setDealAmount(cp.getDealAmount());
        entity.setCommissionStatus(cp.getCommissionStatus());
        entity.setCommissionAmount(commission.toString());
        customerPremisesService.update(entity);
    }
}
